package com.pyl.admin.system.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.pyl.admin.core.enums.ResultEnum;
import com.pyl.admin.core.enums.StatusEnum;
import com.pyl.admin.core.exception.ResultException;
import com.pyl.admin.system.entity.SysDict;
import com.pyl.admin.system.service.SysDictService;
import com.pyl.admin.system.validator.DictForm;
import com.pyl.core.utils.ResultVoUtil;
import com.pyl.core.vo.ResultVo;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 灵魂贰婶
 * @since 2019-06-28
 */
@Controller
@RequestMapping("/system/dict")
public class SysDictController {

	@Autowired
	private SysDictService sysDictService;

	/**
	 * 列表页面
	 */
	@GetMapping("/index")
	@RequiresPermissions("/system/dict/index")
	public String index(Model model, @RequestParam(required = false, defaultValue = "1") Integer status,
			@RequestParam(required = false) String query) {

		// 创建匹配器，进行动态查询匹配
		PageInfo<SysDict> page = sysDictService.selectDictListPage(status, query);
		// 封装数据
		model.addAttribute("list", page.getList());
		model.addAttribute("page", page);
		return "/system/dict/index";
	}

	/**
	 * 跳转到添加页面
	 */
	@GetMapping("/add")
	@RequiresPermissions("/system/dict/add")
	public String toAdd() {
		return "/system/dict/add";
	}

	/**
	 * 跳转到编辑页面
	 */
	@GetMapping("/edit/{id}")
	@RequiresPermissions("/system/dict/edit")
	public String toEdit(@PathVariable("id") Long id, Model model) {
		SysDict sysDict = sysDictService.selectById(id);
		model.addAttribute("dict", sysDict);
		return "/system/dict/add";
	}

	/**
	 * 保存添加/修改的数据
	 *
	 * @param dictForm 表单验证对象
	 */
	@PostMapping({ "/add", "/edit" })
	@RequiresPermissions({ "/system/dict/add", "/system/dict/edit" })
	@ResponseBody
	public ResultVo<?> save(@Validated DictForm dictForm) {
		// 清除字典值两边空格
		dictForm.setValue(dictForm.getValue().trim());
		sysDictService.saveOrUpdate(dictForm);
		return ResultVoUtil.SAVE_SUCCESS;
	}

	/**
	 * 跳转到详细页面
	 */
	@GetMapping("/detail/{id}")
	@RequiresPermissions("/system/dict/detail")
	public String toDetail(@PathVariable("id") Long id, Model model) {
		SysDict dict = sysDictService.selectById(id);
		model.addAttribute("dict", dict);
		return "/system/dict/detail";
	}

	/**
	 * 设置一条或者多条数据的状态
	 */
	@RequestMapping("/status/{param}")
	@RequiresPermissions("/system/dict/status")
	@ResponseBody
	public ResultVo<?> status(@PathVariable("param") String param,
			@RequestParam(value = "ids", required = false) List<Long> idList) {
		try {
			// 获取状态StatusEnum对象
			StatusEnum statusEnum = StatusEnum.valueOf(param.toUpperCase());
			// 更新状态
			boolean result = sysDictService.updateStatus(statusEnum, idList);
			return result ? ResultVoUtil.success(statusEnum.getMessage() + "成功")
					: ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
		} catch (IllegalArgumentException e) {
			throw new ResultException(ResultEnum.STATUS_ERROR);
		}
	}
}
